﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using dotNetDR_OAuth2.APIs;
using dotNetDR_OAuth2.Net;

namespace dotNetDR_OAuth2.Sample.MVC.Controllers
{
    public class DebugToolController : Controller
    {
        [AccessTokenRequired]
        public ActionResult Index()
        {
            return View();
        }

        [AjaxRequired]
        public JsonResult Call(DebugToolFormModel model)
        {
            var result = string.Empty;

            switch (model.wbType)
            {
                case "tencent":
                    try
                    {
                        result = TencentCall(model);
                    }
                    catch (Exception ex)
                    {
                        IApi api = Uf.C(CtorApi.Tencent);

                        var err = api.HandlerException<TencentError>(ex, this.HttpContext);

                        result = err.ToString();
                    }
                    break;
                case "sina":
                    try
                    {
                        result = SinaCall(model);
                    }
                    catch (Exception ex)
                    {
                        IApi api = Uf.C(CtorApi.Sina);

                        var err = api.HandlerException<SinaError>(ex, this.HttpContext);

                        result = err.ToString();
                    }
                    break;
            }

            model.Data = result;

            return Json(new { ret = result });
        }

        [NonAction]
        public string TencentCall(DebugToolFormModel model)
        {
            //fuck tencent 微创新
            var openid = (Session["accessToken"] as dynamic).openid;
            var result = string.Empty;

            IDictionary<string, object> paramsExt = new Dictionary<string, object>();
            paramsExt.Add("openid", openid);

            IApi api = Uf.C(CtorApi.Tencent);
            
            if (model.httpMethod == "get")
            {
                var queryString = string.Empty;
                var apiUrl = model.apiUrl;

                if (model.kv.Count > 0)
                {
                    foreach (KeyValuePair<string, string> item in model.kv)
                    {
                        queryString += string.Format("&{0}={1}", Url.Encode(item.Key), Url.Encode(item.Value));
                    }

                    if (apiUrl.IndexOf('?') < 0)
                    {
                        queryString = queryString.Substring(1);
                        queryString = "?" + queryString;
                    }

                    apiUrl += queryString;
                }


                return api.CallGet(apiUrl, model.accessToken, true, paramsExt);
            }
            else
            {
                if (model.kv.Count > 0)
                {
                    return api.CallPost(model.apiUrl, model.accessToken, model.kv, true, paramsExt);
                }
                else
                {
                    return api.CallPost(model.apiUrl, model.accessToken, null, true, paramsExt);
                }
            }
            return result;
        }

        [NonAction]
        public string SinaCall(DebugToolFormModel model)
        {
            IApi api = Uf.C(CtorApi.Sina);

            var result = string.Empty;

            if (model.httpMethod == "get")
            {
                var queryString = string.Empty;
                var apiUrl = model.apiUrl;

                if (model.kv.Count > 0)
                {
                    foreach (KeyValuePair<string, string> item in model.kv)
                    {
                        queryString += string.Format("&{0}={1}", Url.Encode(item.Key), Url.Encode(item.Value));
                    }

                    if (apiUrl.IndexOf('?') < 0)
                    {
                        queryString = queryString.Substring(1);
                        queryString = "?" + queryString;
                    }

                    apiUrl += queryString;
                }


                result = api.CallGet(apiUrl, model.accessToken, true);
            }
            else
            {
                if (model.kv.Count > 0)
                {


                    Dictionary<string, string> newKv = new Dictionary<string, string>();

                    foreach (KeyValuePair<string, string> item in model.kv)
                    {
                        newKv.Add(Url.Encode(item.Key), Url.Encode(item.Value));
                    }

                    result = api.CallPost(model.apiUrl, model.accessToken, newKv, true);
                }
                else
                {
                    result = api.CallPost(model.apiUrl, model.accessToken, null, true);
                }
            }

            return result;
        }
    }

    public class DebugToolFormModel
    {
        public string accessToken { get; set; }
        public string wbType { get; set; }
        public string httpMethod { get; set; }
        public string apiUrl { get; set; }

        public Dictionary<string, string> kv { get; set; }
        public Dictionary<string, BinaryData> bd { get; set; } //暂未提供

        ////////////////////////

        public string Data { get; set; }
    }
}
